package com.woniuxy.car.commodity.service.core.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.car.commodity.service.core.common.MyAssert;
import com.woniuxy.car.commodity.service.core.dto.CommodityCommentDto;
import com.woniuxy.car.commodity.service.core.entity.CommentPic;
import com.woniuxy.car.commodity.service.core.entity.CommodityComment;
import com.woniuxy.car.commodity.service.core.mapper.CommodityCommentMapper;
import com.woniuxy.car.commodity.service.core.service.ICommentPicService;
import com.woniuxy.car.commodity.service.core.service.ICommodityCommentService;
import com.woniuxy.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author awz
 * @since 2022-03-01
 */
@AllArgsConstructor
@Service
public class CommodityCommentServiceImpl extends ServiceImpl<CommodityCommentMapper, CommodityComment> implements ICommodityCommentService {

    private ICommentPicService commentPicService;

    @Override
    public void addCommodityComment(CommodityCommentDto commodityCommentDto) {
        try {
            MyAssert.notEmpty(commodityCommentDto.getOrderId(), "订单id不能为空");
            MyAssert.notEmpty(commodityCommentDto.getStar(), "评价星级不能为空");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }

        //判断订单是否存在  订单是否处于待评价状态 4   且当前用户是否是订单的创建用户
        //调用订单微服务
        int orderStatus = 4;
        int userId = 1;
        int commodityId = 1;
        if(orderStatus != 4 || userId != commodityCommentDto.getUserId()){
            throw new BusinessException("商品评价失败");
        }
        //创建商品评价对象
        CommodityComment commodityComment = new CommodityComment();
        commodityComment.setOrderId(commodityCommentDto.getOrderId());
        commodityComment.setUserId(userId);
        if(ObjectUtil.isNotEmpty(commodityCommentDto.getContent())){
            commodityComment.setContent(commodityCommentDto.getContent());
        }
        commodityComment.setStar(commodityCommentDto.getStar());
        commodityComment.setReadCount(0);
        commodityComment.setApproveCount(0);
        commodityComment.setReplyCount(0);
        commodityComment.setUserIp(commodityCommentDto.getUserIp());
        commodityComment.setCommodityId(commodityId);
        //将商品评价信息插入数据库
        baseMapper.insert(commodityComment);
        if(ObjectUtil.isNotEmpty(commodityCommentDto.getCommentPics())){
            List<CommentPic> commodityCommentPics = new ArrayList<>();
            List<String> commentPics = commodityCommentDto.getCommentPics();
            for (String pic : commentPics) {
                //创建商品评价图片对象
                CommentPic commentPic = new CommentPic();
                commentPic.setCommentId(commodityComment.getId());
                commentPic.setPicUrl(pic);
                commodityCommentPics.add(commentPic);
            }
            //将商品评价图片对象批量添加到数据库
            commentPicService.saveBatch(commodityCommentPics);
        }

    }
}
